package com.jiyi.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.litepal.crud.DataSupport;

import com.jiyi.bean.message.Message;
import com.jiyi.service.MessageService;
import com.jiyi.service.api.message.MessageApi;
import com.swift.maintenance.base.AppConfig;

public class MessageServiceImpl implements MessageService {

	@Override
	public void responseRecMsg(String access_token, String msgId) {
		Map<String, String> parMap = new HashMap<String, String>();
		parMap.put(AppConfig.ACCESS_TOKEN, access_token);
		parMap.put("id",	msgId);
		MessageApi.responseRecMsg(parMap);
	}

	@Override
	public void getDetailMessage(String token, String hxuuid) {
		Map<String, String> parMap = new HashMap<String, String>();
		parMap.put("token", token);
		parMap.put("hxuuid",	hxuuid);
	}

	@Override
	public List<Message> getLocalMsg() {
		List<Message> temp = DataSupport.where("1=1").order("id desc,createDate desc").find(Message.class);
		return temp;
	}
	
	

	@Override
	public Message getLocalDetailMsg(String hxuuid) {
		Message message = null ;
		List<Message> messageList = DataSupport.where("jyid=?",hxuuid).order("id desc").find(Message.class);
		if (messageList.size() > 0) {
			message = messageList.get(0);
		}
		return message;
	}

	@Override
	public Boolean isNoExist(String jyid) {
		Message message = null ;
		List<Message> messageList = DataSupport.where("jyid=?",jyid).find(Message.class);
		if (messageList.size() > 0) {
			message = messageList.get(0);
		}
		return null == message;
	}

	@Override
	public List<Message> getLocalMsg(String keyword) {
		List<Message> temp = DataSupport.where("msgTitle like ? or msgContent like ?","%"+keyword+"%","%"+keyword+"%").order("id desc,createDate desc").find(Message.class);
		return temp;
	}


	@Override
	public List<Message> getMsgByAccount(String account) {
		List<Message> temp = DataSupport.where("account=? and isDel = ?",account,"0").order("createDate desc,id desc").find(Message.class);
		return temp;
	}

	@Override
	public void deleteMsgByjyid(String jyid) {
		DataSupport.deleteAll(Message.class, "jyid=?", jyid);
	}

	@Override
	public void setMsgReaded(long id) {
		Message message = new Message();
		message.setIsReaded(true);
		message.update(id);
	}

	@Override
	public void setMsgHandle(long id) {
		Message message = new Message();
		message.setIsReaded(true);
		message.setIsHandle(true);
		message.update(id);
	}

	@Override
	public void getNoRecMsgList(String access_token) {
		Map<String, String> parMap = new HashMap<String, String>();
		parMap.put(AppConfig.ACCESS_TOKEN, access_token);
		MessageApi.getNoRecMsgList(parMap);
		

		
	}

	@Override
	public List<Message> getUnReadLocalMsg(String account) {
		List<Message> temp = DataSupport.where("account=? and isDel = ? and isReaded = ?",account,"0","0").find(Message.class);
		return temp;
	}

	@Override
	public Message getLastMessageByType(String account ,String msgType, String belongType,String from) {
		Message message = DataSupport.where("account = ? and msgType = ? and belongType = ? and isDel = 0 and send_user_id = ?",account,msgType,belongType,from).findFirst(Message.class);
		return message;
	}

	@Override
	public void deleteMsgByid(long id) {
		DataSupport.delete(Message.class, id);
	}

	

}
